package com.ssm.service.serviceImpl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ssm.SpecificationDto;
import com.ssm.entity.TbSpecification;
import com.ssm.entity.TbSpecificationExample;
import com.ssm.entity.TbSpecificationOption;
import com.ssm.mapper.TbSpecificationMapper;
import com.ssm.mapper.TbSpecificationOptionMapper;
import com.ssm.service.TbSpecificationService;
import com.ssm.utils.PageResult;

import com.ssm.utils.ToBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class TbSpecificationServiceImpl implements TbSpecificationService {
    private final TbSpecificationMapper tbSpecificationMapper;
    private final TbSpecificationOptionMapper tbSpecificationOptionMapper;

    public TbSpecificationServiceImpl(TbSpecificationMapper tbSpecificationMapper, TbSpecificationOptionMapper tbSpecificationOptionMapper) {
        this.tbSpecificationMapper = tbSpecificationMapper;
        this.tbSpecificationOptionMapper = tbSpecificationOptionMapper;
    }

    @Override
    public PageResult<TbSpecification> conditionFindByPage(Integer page, Integer pageSize, String condition) {
        PageHelper.startPage(page, pageSize);
        TbSpecificationExample tbSpecificationExample = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = tbSpecificationExample.createCriteria();
        if (StrUtil.isNotBlank(condition)) {
            criteria.andSpecNameLike("%" + condition + "%");
        }
        Page<TbSpecification> tbSpecifications = (Page<TbSpecification>) tbSpecificationMapper.selectByExample(tbSpecificationExample);
        if (tbSpecifications != null) {
            return new PageResult<>(tbSpecifications,  tbSpecifications.getTotal());
        }
        return new PageResult<>(Collections.emptyList(), 0L);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addTbSpecification(SpecificationDto specificationDto) {
        TbSpecification tbSpecification = specificationDto.getSpecification();
        if (tbSpecification == null) {
            return;
        }
        tbSpecificationMapper.insert(tbSpecification);
        TbSpecificationExample tbSpecificationExample = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = tbSpecificationExample.createCriteria();
        criteria.andSpecNameEqualTo(tbSpecification.getSpecName());
        List<TbSpecification> tbSpecifications = tbSpecificationMapper.selectByExample(tbSpecificationExample);
        tbSpecification.setId(tbSpecifications.get(0).getId());
        List<TbSpecificationOption> specificationOptionList =  specificationDto.getSpecificationOptionList();
        if(specificationOptionList!=null&&!specificationOptionList.isEmpty()) {
            specificationOptionList.forEach(tbSpecificationOption -> {
                tbSpecificationOption.setSpecId(tbSpecification.getId());
            });
            tbSpecificationOptionMapper.batchInsert(specificationOptionList);
        }

    }
}
